package com.ansatsing.templateMethod.withHook;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//泡茶：1把水煮沸--》2用沸水浸泡茶叶--》3把茶水倒进杯子---》4加柠檬
//步骤1和步骤3在模版类里实现了
public class Tea extends RecipeTemplate {
	
	@Override
	void brew() {//2用沸水浸泡茶叶
		System.out.println("Steeping the tea");
	}

	@Override
	void addCondiments() {//4加柠檬
		System.out.println("Adding Lemon");
	}
	@Override
	boolean customerWantsCondiments() {//客户自己决定是否加调料--钩子方法重写
		String answer = getUserInput();

		if (answer.toLowerCase().startsWith("y")) {
			return true;
		} else {
			return false;
		}
	}
	private String getUserInput() {//接受键盘输入
		String answer = null;

		System.out.print("Would you like milk and sugar with your coffee (y/n)? ");

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try {
			answer = in.readLine();
		} catch (IOException ioe) {
			System.err.println("IO error trying to read your answer");
		}
		if (answer == null) {
			return "no";
		}
		return answer;
	}	
}
